--INSERT INTO cat_material
SELECT 
case len(convert(varchar(10),companyid))
    when 5 then '00000000-0000-0000-0000-0000000' + convert(varchar(10),companyid) 
    when 4 then '00000000-0000-0000-0000-00000000' + convert(varchar(10),companyid) 
    when 3 then '00000000-0000-0000-0000-000000000' + convert(varchar(10),companyid) 
    when 2 then '00000000-0000-0000-0000-0000000000' + convert(varchar(10),companyid) 
    when 1 then '00000000-0000-0000-0000-00000000000'+ convert(varchar(10),companyid) 
    end AS [Id],Title AS [Description],2 AS [MaterialType],NULL AS [Text],NULL AS [TextStyle],Url AS [NavigateUrl],1 AS [Target],NULL AS [ImageUrl],AdImage AS [ImageBinary],case imagetypewhen '.jpg' then 'image/jpeg'when '.gif' then 'image/gif'end AS [MimeType],115 AS [Width],52 AS [Height],title AS [AlternateText],NULL AS [FlashUrl],NULL AS [IsTransparency],NULL AS [FlashBinary],note AS [Notes],1 AS [AreaCode],createddate AS [DateCreated],updateddate AS [LastUpdated]FROM srv_lnk.nchr2008.dbo.ad
where  id in(SELECT max(id) FROM srv_lnk.nchr2008.dbo.ad group by companyid)
order by Expireddate desc


